<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.cleanbing.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 原子力量 <http://www.cleanbing.cn>
// +----------------------------------------------------------------------

namespace Desk\Controller;

/**
 * 试驾申请控制器
 * @author 原子力量 <http://www.cleanbing.cn>
 */
class TestDriveController extends ThinkController {/*注意是集成ThinkController而不是AdminController*/
    private $model_name = 'testdrive'; /*在OneThink模型管理中查看自己模型标识（不是名称）修改此处*/

    /**
     * 试驾申请管理首页
     * @author 原子力量 <http://www.cleanbing.cn>
     */
    public function index(){
        $this->lists(); /*系统会调用View/TestDrive/index.html来显示*/
    }

    public function lists(){
 
        $page = 1; //默认显示第一页数据

        //获取模型信息
        $model = M('Model')->getByName( $this->model_name );
        $model || $this->error('TestDrive-lists: 模型不存在！');

        //解析列表规则
        $fields = array();
        $grids  = preg_split('/[;\r\n]+/s', trim($model['list_grid']));
        foreach ($grids as &$value) {
            if(trim($value) === ''){
                continue;
            }
            // 字段:标题:链接
            $val      = explode(':', $value);
            // 支持多个字段显示
            $field   = explode(',', $val[0]);
            $value    = array('field' => $field, 'title' => $val[1]);
            if(isset($val[2])){
                // 链接信息
                $value['href']  =   $val[2];
                // 搜索链接信息中的字段信息
                preg_replace_callback('/\[([a-z_]+)\]/', function($match) use(&$fields){$fields[]=$match[1];}, $value['href']);
            }
            if(strpos($val[1],'|')){
                // 显示格式定义
                list($value['title'],$value['format'])    =   explode('|',$val[1]);
            }
            foreach($field as $val){
                $array  =   explode('|',$val);
                $fields[] = $array[0];
            }
        }

        // 关键字搜索
        // $map    =   array();
        // $key    =   $model['search_key']?$model['search_key']:'title';
        // if(isset($_REQUEST[$key])){
        //     $map[$key]  =   array('like','%'.$_GET[$key].'%');
        //     unset($_REQUEST[$key]);
        // }

        $row    = empty($model['list_row']) ? 10 : $model['list_row'];

        //get data of Model
            if($model['need_pk']){
                in_array('id', $fields) || array_push($fields, 'id');
            }
            $name = parse_name( $this->model_name );
            $data = M($name)
                /* 查询指定字段，不指定则查询所有字段 */
                ->field(empty($fields) ? true : $fields)
                /* 默认通过id逆序排列 */
                ->order($model['need_pk']?'id DESC':'')
                /* 数据分页 */
                ->page($page, $row)
                /* 执行查询 */
                ->select();

            /* 查询记录总数 */
            $count = M($name)->count();


        //分页
        if($count > $row){
            $page = new \Think\Page($count, $row);
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $this->assign('_page', $page->show());
        }

        $data   =   $this->parseDocumentList($data,$model['id']);
        $this->assign('model', $model);
        $this->assign('list_grids', $grids);
        $this->assign('list_data', $data);
        $this->meta_title = $model['title'].'列表';
        $this->display('lists');
    }

    public function add( $model = null ){
        $model = M('Model')->getByName( $this->model_name ); /*通过Model名称获取Model完整信息*/
        parent::add( $model['id'] ); /*系统会调用View/TestDrive/add.html来显示*/
    }

    public function edit( $model = null, $id = 0 ){
        dump('TestDrive:Edit');
        $id || $this->error('请选择要编辑的用户！');
        $model = M('Model')->getByName( $this->model_name ); /*通过Model名称获取Model完整信息*/
        parent::edit( $model['id'], $id ); /*系统会调用View/TestDrive/edit.html来显示*/
    }
 
    public function del( $model = null, $ids=null ){
        dump('del');
        $model = M('Model')->getByName( $this->model_name ); /*通过Model名称获取Model完整信息*/
        parent::del( $model['id'], $ids ); /*没有页面，只有Ajax提示返回，不需要View/Team/del.html*/
    }
}
